﻿Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine

Public Class frmTargetStaffEntry

    Sub FillStaffList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim ProducersList As ArrayList = GetProducersList()

            Me.ListUsers.Items.Clear()
           
            For Each Producer As String In ProducersList
                Dim Item As New ListViewItem
                Item.Text = Producer

                Me.ListUsers.Items.Add(Item)
            Next

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ListUsers_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListUsers.SelectedIndexChanged
        Me.txtSalesman.Clear()
        Me.GridBudget.Rows.Clear()

        If Me.ListUsers.SelectedItems.Count > 0 Then
            Me.txtSalesman.Text = Me.ListUsers.SelectedItems(0).Text
            FillBudget()
        End If
    End Sub

    Sub FillBudget()
        Try
            Me.Cursor = Cursors.WaitCursor

            Me.GridBudget.Rows.Clear()

            Dim cmd As New SqlCommand
            cmd.Connection = cnn

            cnn.Open()
            For i As Integer = 1 To 12
                cmd.CommandText = "Select IsNull(dbo.GetPremTargetStaff(" & Me.DTPYear.Value.Year & "," & i & _
                                  ",N'" & Me.txtSalesman.Text.Trim & "'),0)"

                Me.GridBudget.Rows.Add(New String() {i, MonthName(i), CDbl(cmd.ExecuteScalar).ToString("N2")})
            Next
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub DTPYear_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DTPYear.ValueChanged
        If Me.ListUsers.SelectedItems.Count > 0 Then
            FillBudget()
        End If
    End Sub

    Private Sub frmTargetStaff_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized

        FillStaffList()
    End Sub

    Private Sub GridBudget_CellEndEdit(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles GridBudget.CellEndEdit
        Try
            Me.GridBudget.CurrentCell.Value = CDbl(Me.GridBudget.CurrentCell.Value).ToString("N2")
        Catch ex As Exception
            Me.GridBudget.CurrentCell.Value = "0.00"
        End Try
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Me.ErrProv.Clear()

        If Me.ListUsers.SelectedItems.Count = 0 Then
            Me.ErrProv.SetError(Me.txtSalesman, "Please select a valid salesman from the list")
        Else
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand
                Dim Trans As SqlTransaction

                cmd.Connection = cnn

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Transaction = Trans

                'Delete old data
                cmd.CommandText = "Delete From StaffTarget Where Year=" & Me.DTPYear.Value.Year & _
                                  " and SalesMan=N'" & Me.txtSalesman.Text.Trim & "'"
                cmd.ExecuteNonQuery()

                'Save new year budget
                For Each row As DataGridViewRow In Me.GridBudget.Rows
                    cmd.CommandText = "Insert Into StaffTarget (Year,Month,SalesMan,Target) " & _
                                      "Values (@Year,@Month,@SalesMan,@Target)"

                    cmd.Parameters.Clear()
                    cmd.Parameters.AddWithValue("@Year", Me.DTPYear.Value.Year)
                    cmd.Parameters.AddWithValue("@Month", row.Cells(0).Value)
                    cmd.Parameters.AddWithValue("@SalesMan", Me.txtSalesman.Text.Trim)
                    cmd.Parameters.AddWithValue("@Target", CDbl(row.Cells(2).Value))
                    cmd.ExecuteNonQuery()
                Next

                Trans.Commit()
                cnn.Close()

                MsgBox("Saved Successfully")

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub
End Class